package cn.edu.cuit.elena.trace;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Tracer
{
    private volatile static boolean TRACE_ENABLE = true;
    @SuppressWarnings( "unused" )
    private static Level LEVEL = Level.ALL;
    static
    {
        PropertyConfigurator.configure( ClassLoader.getSystemResource( "log4j.properties" ) );
        if( System.getProperty( "TRACE_ENABLE" ) == "false" )
        {
            TRACE_ENABLE = false;
        }
    }

    public static void TraceDisable()
    {
        TRACE_ENABLE = false;
    }

    public static void debugTrace( Class<? extends Object> Modelclazz, String message, Throwable e )
    {
        Logger logger;
        if( TRACE_ENABLE )
        {
            logger = Logger.getLogger( Modelclazz );
            String clazzName = formatClassName( 35, Modelclazz.toString() );

            if( e == null )
            {
                logger.debug( message );
                System.out.println( clazzName + " | " + message );
            }
            else
            {
                logger.debug( message, e );
                System.out.println( clazzName + " | " + message + " | " + e );
            }
        }
    }

    public static void logTrace( Class<? extends Object> Modelclazz, String message, Throwable e )
    {
        Logger logger;
        if( TRACE_ENABLE )
        {
            logger = Logger.getLogger( Modelclazz );
            String clazzName = formatClassName( 35, Modelclazz.toString() );
            if( e == null )
            {
                logger.debug( message );
                System.out.println( clazzName + " | " + message );
            }
            else
            {
                logger.debug( message, e );
                System.out.println( clazzName + " | " + message + " | " + e );
            }
        }
    }

    public static void infoTrace( Class<? extends Object> Modelclazz, String message, Throwable e )
    {
        Logger logger;
        if( TRACE_ENABLE )
        {
            logger = Logger.getLogger( Modelclazz );
            String clazzName = formatClassName( 35, Modelclazz.toString() );
            if( e == null )
            {
                logger.debug( message );
                System.out.println( clazzName + " | " + message );
            }
            else
            {
                logger.debug( message, e );
                System.out.println( clazzName + " | " + message + " | " + e );
            }
        }
    }

    public static void warnTrace( Class<? extends Object> Modelclazz, String message, Throwable e )
    {
        Logger logger;
        if( TRACE_ENABLE )
        {
            logger = Logger.getLogger( Modelclazz );
            String clazzName = formatClassName( 35, Modelclazz.toString() );
            if( e == null )
            {
                logger.debug( message );
                System.out.println( clazzName + " | " + message );
            }
            else
            {
                logger.debug( message, e );
                System.out.println( clazzName + " | " + message + " | " + e );
            }
        }
    }

    public static void fatalTrace( Class<? extends Object> clazz, String message, Throwable e )
    {
        Logger logger;
        if( TRACE_ENABLE )
        {
            logger = Logger.getLogger( clazz );
            String clazzName = formatClassName( 35, clazz.toString() );
            if( e == null )
            {
                logger.debug( message );
                System.out.println( clazzName + " | " + message );
            }
            else
            {
                logger.debug( message, e );
                System.out.println( clazzName + " | " + message + " | " + e );
            }
        }
    }

    private static String formatClassName( int count, String message )
    {
        message = message.substring( 24, message.length() );
        if( message.length() < count )
        {
            int blank = count - message.length();
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append( message );
            for( int i = 0; i < blank; i++ )
            {
                stringBuilder.append( " " );
            }

            return new String( stringBuilder );

        }

        return message;
    }
}
